<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>添加节点</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" type="text/css" href="../../css/sm.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/normal.css" />
    <link rel="stylesheet" type="text/css" href="../../css/addPoint.css" />
    <link rel="stylesheet" type="text/css" href="../../iconfont/iconfont.css" />
</head>

<body>
    <div class="page allWarp">
        <div class="content">
            <div class="pointContent">
                <ul>
                    <li>
                        <span class="width30">节点名称</span>
                        <span class="width70">
                            <input type="text" placeholder="请填写节点名称" class="width100 textRight pright10" v-model="nodeName" >
                        </span>
                    </li>
                    <li>
                        <span class="width30">负责人</span>
                    </li>
                    <div style="padding: 0.625rem; ">
                        <span style="width: 25%; display: flex; float: left;margin-bottom: 0.3125rem; align-items: center; position: relative;" v-for="item in listUser" >
                            <span v-text="item.dutyUser" class="userTx"></span>
                            <i class="iconfont icon-youjiantou left10"></i>
                            <i class="iconfont icon-shanchu1" onclick="removeSession()" :data-dutyUserId = "item.dutyUserId" style="position: absolute;height: 1rem; top: -0.3rem; left: 1.8rem;"></i>
                        </span>
                        <span style="width: 25%;float: left;display: flex; align-items: center; line-height: normal;margin-bottom: 0.3125rem;">
                            <i class="iconfont icon-icon02 right10 addUser" style="float: left" onclick="moveTo('dutyUser')"></i>
                        </span>
                    </div>
                </ul>
            </div>
            <div class="save" onclick="moveTo('save')">保存</div>
        </div>
    </div>
    <script type="text/javascript" src="../../js/zepto.js"></script>
    <script type="text/javascript" src="../../js/sm.min.js"></script>
    <script type="text/javascript" src="../../js/vue.min.js"></script>
    <script type="text/javascript" src="../../js/ajax.js"></script>
    <script type="text/javascript" src="../../js/back.js"></script>
    <script type="text/javascript">
        var nodeCode = GetQueryString("nodeCode");
        var come = GetQueryString("come");
        var role = GetQueryString("role");
        var projectCode = GetQueryString("projectCode");
        var dutyUserId = GetQueryString("dutyUserId");
        var isNewNode = GetQueryString("isNewNode");//判断是否为新建节点
        var nodeSaler = JSON.parse(sessionStorage.getItem('nodeSaler_data'))
        var node_data = JSON.parse(sessionStorage.getItem('node_data'));
        var keyN = JSON.parse(getCookie("user_data_two"));
        var createUid;
        var customerPhone;
        console.log(nodeSaler)
        if (keyN != null) {
            createUid = keyN.userId;
        }
        var demo = new Vue({
            el: '.allWarp',
            data: {
                nodeName: node_data ==null ? '' : node_data.nodeName,      //节点名称
                dutyUser:nodeSaler == null ? '':nodeSaler.salerName,        //负责人姓名
                dutyUserId:nodeSaler == null ? '':nodeSaler.dutyUserId,      //负责人编码
                dutyPhone:nodeSaler == null ? '':nodeSaler.salerPhone,       //负责人手机号码
                nodeCode:'',        //节点编码（修改必填）
                projectCode:'',     //项目编码
                listUser:[],
                nodeDutyList:'',
                status:true,
            },
            beforeCreate: function() {
                console.log(isNewNode)
                if(isNewNode == "false" && nodeSaler == null){
                    init();
                }
            }
        })
        demo.listUser = nodeSaler;
        // 节点列表
        function init(){
            var params = {};
            params.nodeCode = nodeCode;
            kfAjaxGet.nodeDetail(params, function(json) {
                if (json.code == 200) {
                    var data = json.data;
                    demo.nodeName = data.nodeName;
                    demo.listUser = data.nodeDutyList;

                    console.log(demo.listUser)
                } else if (checkcode(json.code)) {

                } else {
                    $.toast(json.message);
                }
            })
        }
        //跳转
        function moveTo(val){
            if(val == "dutyUser"){
                var newnodesaler = [];
                console.log(demo.listUser)
                if (demo.listUser != null) {
                    for (var i=0;i<demo.listUser.length;i++){
                        newnodesaler.push(demo.listUser[i])
                    }
                    demo.listUser = newnodesaler;
                    sessionStorage.setItem('nodeSaler_data', JSON.stringify(newnodesaler))
                }
                
                var data = {};
                data.nodeName = demo.nodeName;
                sessionStorage.setItem('node_data', JSON.stringify(data))
                window.location = "chooseSaler.html?isNewNode="+isNewNode+'&projectCode='+projectCode+'&nodeCode='+nodeCode+'&come='+come+'&userType=3'+'&role='+role;
            }
            if(val == "save"){
                if (!demo.nodeName) {
                    $.toast('请填写节点名称');
                    return false;
                }
                if (demo.listUser.length == 0) {
                    $.toast('请选择负责人');
                    return false;
                }
                var dutyUserIds = [];
                for (var i=0;i<demo.listUser.length;i++){
                    dutyUserIds[i] = demo.listUser[i].dutyUserId;
                }
                dutyUserIds = dutyUserIds.join(',');
                if(!demo.status){
                    return;
                }
                demo.status = false;

                var params = {};
                params.projectCode = projectCode;
                params.nodeCode = nodeCode;
                params.nodeName = demo.nodeName;
                params.dutyUserIds = dutyUserIds;
                params.createUid = createUid;
                kfAjaxGet.editNode(params, function(json) {
                    if (json.code == 200) {
                        demo.status = true;
                        sessionStorage.removeItem("nodeSaler_data");
                        // return;
                        if (come == 'an') {
                            window.location = "creatProject2.html?projectCode="+projectCode+'&role='+role;
                        } else if(come == 'cp'){
                            window.location = "creatProject2.html?projectCode="+projectCode+'&role='+role;
                        }else if(come == 'anp'){
                            window.location = "creatProject2.html?projectCode="+projectCode+'&role='+role;
                        }
                        
                    } else if (checkcode(json.code)) {
                        demo.status = true;
                    } else {
                        demo.status = true;
                        $.toast(json.message);
                    }
                })
            }
        }

        function removeSession(){
            var _that = $(event.currentTarget);
            var dutyUserId = _that.attr("data-dutyUserId");
            var newnodesaler = [];
            for (var i=0;i<demo.listUser.length;i++){
                if (demo.listUser[i].dutyUserId == dutyUserId) {

                }else{
                    newnodesaler.push(demo.listUser[i])
                }
            }
            demo.listUser = newnodesaler;
            sessionStorage.setItem('nodeSaler_data', JSON.stringify(newnodesaler))
        }

        $(function(){
            $('#gohome').on('click',function(){
                window.location = 'myProject.html?role='+role;
            })
        })
    </script>
</body>
</html>